package com.wrs.project.module.vlc.view

import android.content.Context
import android.graphics.SurfaceTexture
import android.net.Uri
import android.util.AttributeSet
import android.view.TextureView
import android.view.View
import android.widget.FrameLayout
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer

open class VLCView : FrameLayout {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        init()
    }

     var mMediaPlayer: MediaPlayer? = null
    var mTextureView: TextureView? = null

     var eventCallback: ((event: MediaPlayer.Event)->Unit)? = null
    var  onLoadPlayer: ((player: MediaPlayer)-> Unit)? = null

    fun init() {
    }

    fun setUrl(url: String, options: List<String>, autoPlay: Boolean) {
         releaseVLC()
         clearSubView()
        val txtureView= TextureView(context)
        mTextureView = txtureView
        txtureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(
                p0: SurfaceTexture,
                p1: Int,
                p2: Int
            ) {
                 createVLC(txtureView, p0, url, options, autoPlay)
            }

            override fun onSurfaceTextureSizeChanged(
                p0: SurfaceTexture,
                p1: Int,
                p2: Int
            ) {

            }

            override fun onSurfaceTextureDestroyed(p0: SurfaceTexture): Boolean {
                return false
            }

            override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {

            }

        }
        val layout = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        addView(txtureView, layout)
    }

    fun createVLC(textureView: TextureView, surfaceTexture: SurfaceTexture, url: String, options: List<String>, autoPlay: Boolean) {
        val libVLC = LibVLC(this.context, options)
        // 4.0.0-dev Otto Chriek
        val version = LibVLC.version()
        // 4
//        val majorVersion = LibVLC.majorVersion()
        val mediaPlayer = MediaPlayer(libVLC)

        mMediaPlayer = mediaPlayer
        mediaPlayer.setEventListener(object : MediaPlayer.EventListener {
            override fun onEvent(event: MediaPlayer.Event?) {
                event?.let { e ->
                    eventCallback?.let { callback ->
                        callback(e)
                    }
                }

            }
        })
        mediaPlayer.vlcVout.setVideoSurface(surfaceTexture)
        mediaPlayer.vlcVout.setWindowSize(textureView.width, textureView.height)
        textureView.addOnLayoutChangeListener(object : OnLayoutChangeListener{
            override fun onLayoutChange(
                p0: View?,
                left: Int,
                top: Int,
                right: Int,
                botton: Int,
                p5: Int,
                p6: Int,
                p7: Int,
                p8: Int
            ) {
                mediaPlayer.vlcVout.setWindowSize(right - left, botton-top)
            }
        })
        mediaPlayer.vlcVout.attachViews()
        var media: Media
        if (url.startsWith("/")) {
            media =  Media(libVLC, url)
        } else { // rtmp、http、rtsp
            media =  Media(libVLC, Uri.parse(url))
        }
        mediaPlayer.media = media
        media.release()

        if (autoPlay) {
            mediaPlayer.play()
        }
        onLoadPlayer?.let {
            it(mediaPlayer)
        }
    }

    fun releaseVLC() {
        mMediaPlayer?.let {
            if (it.isPlaying) {
                it.stop()
            }
            it.libVLC.release()
            it.detachViews()
            it.release()
            mMediaPlayer = null
        }
    }

    fun clearSubView(){
        removeAllViews()
    }
}
